Component({
  data: {
    selected: 0,
    color: "#999999",
    selectedColor: "#1296db",
    list: [],
    show: true
  },

  attached() {
    this.setData({
      list: this.getTabBarList()
    });
    this.show();
  },

  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const url = data.path;
      wx.switchTab({
        url
      });
      this.setData({
        selected: data.index
      });
    },

    getTabBarList() {
      const userInfo = wx.getStorageSync('userInfo');
      const role = userInfo ? userInfo.role : null;
      
      // 基础 tabBar 配置
      const baseTabBar = {
        color: this.data.color,
        selectedColor: this.data.selectedColor,
        list: []
      };

      // 根据角色返回不同的 tabBar 配置
      if (role === 1) { // 教师
        baseTabBar.list = [
          {
            pagePath: "/pages/course/course",
            text: "首页",
            iconPath: "/assets/images/home.png",
            selectedIconPath: "/assets/images/home_selected.png"
          },
          {
            pagePath: "/pages/my/my",
            text: "我的",
            iconPath: "/assets/images/my.png",
            selectedIconPath: "/assets/images/my_selected.png"
          }
        ];
      } else if (role === 2) { // 学生
        baseTabBar.list = [
          {
            pagePath: "/pages/student_home/student_home",
            text: "首页",
            iconPath: "/assets/images/home.png",
            selectedIconPath: "/assets/images/home_selected.png"
          },
          {
            pagePath: "/pages/my/my",
            text: "我的",
            iconPath: "/assets/images/my.png",
            selectedIconPath: "/assets/images/my_selected.png"
          }
        ];
      }

      return baseTabBar.list;
    },

    show() {
      // 获取当前页面
      const pages = getCurrentPages();
      const currentPage = pages[pages.length - 1];
      const route = currentPage.route;
      
      // 在这些页面不显示 tabBar
      const hideInPages = ['pages/index/index', 'pages/login/login', 'pages/register/register'];
      
      if (hideInPages.some(page => route.includes(page))) {
        this.setData({ show: false });
      } else {
        this.setData({ 
          show: true,
          list: this.getTabBarList()
        });
        
        // 设置当前选中的 tab
        const list = this.data.list;
        for (let i = 0; i < list.length; i++) {
          if (route.includes(list[i].pagePath)) {
            this.setData({ selected: i });
            break;
          }
        }
      }
    }
  }
}); 